package com.mxx.common.generator;

import com.alibaba.fastjson.annotation.JSONField;
import com.baomidou.mybatisplus.generator.config.ConstVal;
import com.mxx.common.conv.JsonUtil;
import com.mxx.common.core.UniquelyList;
import com.mxx.common.generator.convert.*;
import com.mxx.common.generator.db.DBFileGeneratorManagerBuilder;
import com.mysql.cj.MysqlType;
import lombok.Data;
import lombok.experimental.Accessors;
import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.VelocityEngine;

import javax.persistence.Column;
import java.io.FileWriter;
import java.io.Serializable;
import java.util.*;

public class Test {


    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://192.168.0.100:3306/db_zhanye?characterEncoding=utf8&useSSL=false&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true&zeroDateTimeBehavior=convertToNull";
        String username = "root";
        String password = "hzsy123";

        EntityWriter.loadDefault();

        GlobalNameConfig globalNameConfig = new GlobalNameConfig()
                .removeClassNamePrefix("tb")
                .setEntityPackage("com.mxx.common.files")
                .setClassNameSuffix("Entity");


        DBFileGeneratorManagerBuilder builder = DBFileGeneratorManagerBuilder.builder(url, username, password);
        FileGeneratorManager generatorManager = builder.registerTypeConvert(LocalDateTimeTypeConvert.INSTANCE)
                .setGlobalNameConfig(globalNameConfig)
                .complete();

        UniquelyList<String, EntityInfo> entityInfos = generatorManager.generator();


        EntityInfo superInter = new EntityInfo(Serializable.class);
        EntityAnno annotation = new EntityAnno(Accessors.class);
        annotation.addValue("chain", true);
        annotation.afterPropertiesSet();

        for (EntityInfo entityInfo : entityInfos) {
            entityInfo.addSuperInterface(superInter);
            entityInfo.addAnno(annotation);

            List<EntityField> entityFields = new ArrayList<>();
            entityFields.add(new EntityField().setFieldName("id").setMysqlType(MysqlType.VARCHAR));
            LinkedHashSet<EntityInfo> set = new LinkedHashSet<>();
            EntityInfo interClass = new EntityInfo(Constant.EntityType.CLASS)
                    .addSuperInterface(superInter)
                    .setEntityState(Constant.EntityState.STATIC)
                    .setEntityName(entityInfo.getEntityName().replace("Entity", "") + "Id")
                    .setEntityFieldList(entityFields);

            interClass.afterPropertiesSet();
            set.add(interClass);

            entityInfo.setInnerClass(set);
            entityInfo.getEntityFieldList().forEach(field -> {
                field.addAnno(new EntityAnno(Column.class)
                        .addValue("name", field.getTableColumn().getColumnName())
                        .addValue("nullable", false)
                        .addValue("length", field.getTableColumn().getColumnSize())
                        .afterPropertiesSet())
                        .addAnno(new EntityAnno(JSONField.class)
                                .addValue("name", field.getFieldName())
                                .afterPropertiesSet());
            });

            entityInfo.afterPropertiesSet();
            EntityWriter.writer(entityInfo, "E:/a.java");
            break;
        }

        System.out.println("");
    }

}
